#include "main.h"
#include "Ano_Data.h"
#include "path.h"
#include "HMI.h"
#include "stdio.h"
#include "usart.h"


uint8_t flag_drawpathline=0;
uint8_t flag_recved_animal=0;
extern uint8_t HMI_rx_buffer[5];       //!0xAA 0x00 0x00 0x00 0x55
extern uint8_t UAV_rx_buffer[9];
uint8_t line[100][2] = {{0,0}};


uint16_t location_map[7][9][2] = 
{
    {{46,415},{115,415},{184,415},{254,415},{323,415},{393,415},{462,415},{531,415},{601,415}},
    {{46,346},{115,346},{184,346},{254,346},{323,346},{393,346},{462,346},{531,346},{601,346}},
    {{46,276},{115,276},{184,276},{254,276},{323,276},{393,276},{462,276},{531,276},{601,276}},
    {{46,208},{115,208},{184,208},{254,208},{323,208},{393,208},{462,208},{531,208},{601,208}},
    {{46,138},{115,138},{184,138},{254,138},{323,138},{393,138},{462,138},{531,138},{601,138}},
    {{46,69},{115,69},{184,69},{254,69},{323,69},{393,69},{462,69},{531,69},{601,69}},
    {{46,0},{115,0},{184,0},{254,0},{323,0},{393,0},{462,0},{531,0},{601,0}}
};









//void update_location()
//{
//    printf("n0.val=%d\xff\xff\xff",location_act[0]);
//    printf("n1.val=%d\xff\xff\xff",location_act[1]);
//}

void set_line(uint8_t line_array[100][2],uint8_t len,char * color)
{
    uint8_t x_last,y_last,x,y;

    for(uint8_t i = 0; i < len; i++)
    {
        x_last =9- line_array[i][0];
        y_last =1+ line_array[i][1];
        x =9- line_array[i+1][0];
        y =1+ line_array[i+1][1];
        if(i==len-1 && i !=0)
        {
            x = line_array[i][0];
            y = line_array[i][1];
        }
        printf("line %d,%d,%d,%d,%s\xff\xff\xff",
				location_map[y_last-1][x_last-1][0] + 34 ,location_map[y_last-1][x_last-1][1] + 34,
				location_map[y-1][x-1][0] + 34 ,location_map[y-1][x-1][1] + 34,color);
				HAL_Delay(500);
    }
    printf("line %d,%d,%d,%d,%s\xff\xff\xff",
        location_map[y_last][x_last][0] + 34 ,location_map[y_last][x_last][1] + 34,
        location_map[y][x][0] + 34 ,location_map[y][x][1] + 34,color);


}

void update_animals_num()
{
    printf("n2.val=%d\xff\xff\xff",peacock.munber);
    printf("n3.val=%d\xff\xff\xff",monkey.munber);
    printf("n4.val=%d\xff\xff\xff",wolf.munber);
    printf("n5.val=%d\xff\xff\xff",tiger.munber);
    printf("n6.val=%d\xff\xff\xff",elephant.munber);
}
void update_animals_location()
{

}

void Init(void);
void set_nofly_area(uint8_t swhi,uint8_t x, uint8_t y)
{
    switch (swhi)
    {
    case 1:
        printf("p1.x=%d\xff\xff\xff",location_map[y-1][x-1][0]);
        printf("p1.y=%d\xff\xff\xff",location_map[y-1][x-1][1]);
        break;
    case 2:
        printf("p2.x=%d\xff\xff\xff",location_map[y-1][x-1][0]);
        printf("p2.y=%d\xff\xff\xff",location_map[y-1][x-1][1]);
        break;
    case 3:
        printf("p3.x=%d\xff\xff\xff",location_map[y-1][x-1][0]);
        printf("p3.y=%d\xff\xff\xff",location_map[y-1][x-1][1]);
        break;
    default:
        break;
    }
}


//uint8_t p_is(uint8_t x,uint8_t y)
//{
//    if(x == p1[0] && y == p1[1])
//        return 1;
//    else if(x == p2[0] && y == p2[1])
//        return 2;
//    else if(x == p3[0] && y == p3[1])
//        return 3;
//    return 0;

//}

uint8_t RX_str=0;
uint8_t RX_HMI_str=0;
	//!当前航点
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart == &huart2)
  {
		HMIRxOneByte(RX_HMI_str);
    HAL_UART_Receive_IT(&huart2, &RX_HMI_str,1);
  }
  if(huart == &huart3) 
	{
    AnoDTRxOneByte(RX_str);
		
		HAL_UART_Receive_IT(&huart3,&RX_str,1);
	}
}

void Init(void)
{

	HAL_UART_Receive_IT(&huart2, &RX_HMI_str,1);
	HAL_UART_Receive_IT(&huart3, &RX_str, 1);

    peacock.No1.p_id = 1;
    peacock.No2.p_id = 2;
    peacock.No3.p_id = 3;
    monkey.No1.p_id = 4;
    monkey.No2.p_id = 5;
    monkey.No3.p_id = 6;
    wolf.No1.p_id = 7;
    wolf.No2.p_id = 8;
    wolf.No3.p_id = 9;
    tiger.No1.p_id = 10;
    tiger.No2.p_id = 11;
    tiger.No3.p_id = 12;
    elephant.No1.p_id = 13;
    elephant.No2.p_id = 14;
    elephant.No3.p_id = 15;

    peacock.No1.t_id = 16;
    peacock.No2.t_id = 17;
    peacock.No3.t_id = 18;
    monkey.No1.t_id = 19;
    monkey.No2.t_id = 20;
    monkey.No3.t_id = 21;
    wolf.No1.t_id = 22;
    wolf.No2.t_id = 23;
    wolf.No3.t_id = 24;
    tiger.No1.t_id = 25;
    tiger.No2.t_id = 26;
    tiger.No3.t_id = 27;
    elephant.No1.t_id = 28;
    elephant.No2.t_id = 29;
    elephant.No3.t_id = 30;

    peacock.No1.is = 0;
    peacock.No2.is = 0;
    peacock.No3.is = 0;
    monkey.No1.is = 0;
    monkey.No2.is = 0;
    monkey.No3.is = 0;
    wolf.No1.is = 0;
    wolf.No2.is = 0;
    wolf.No3.is = 0;
    tiger.No1.is = 0;
    tiger.No2.is = 0;
    tiger.No3.is = 0;
    elephant.No1.is = 0;
    elephant.No2.is = 0;
    elephant.No3.is = 0;

    peacock.munber = 0;
    monkey.munber = 0;
    wolf.munber = 0;
    tiger.munber = 0;
    elephant.munber = 0;
}

void entrance()
{
    Init();
//    set_animals(peacock.No1.p_id,5,4);//show  a wolf
		FlyDataSend(10,0x01);
uint32_t cnt=0;
    while(1)
    {
//			UAV_location_relize(UAV_rx_buffer);
			cnt++;
			if(cnt%10==0)
			{
			printf("n0.txt=\"%d\"\xff\xff\xff",UAV.x);
			printf("n1.txt=\"%d\"\xff\xff\xff",UAV.y);

			}
			if(flag_drawpathline)
				{
					path_design_init();
					for(uint8_t i = 0;i<100;i++)
					{
							line[i][0] = path[i][0];
							line[i][1] = path[i][1];
					}
					for (int location_num=0;location_num<path_index;){
							
							FlyDataSend(location_num,GO_Mode);
							flag_recved_animal=0;
							HAL_Delay(200);
							if( (animal.Loca_x==1) && (animal.Loca_y==1) )
							{
								flag_recved_animal=1;
							}
							printf("t7.txt=\"A%d,B%d\"\xff\xff\xff",10-(line[location_num][0]+1),line[location_num][1]+1);
							if(flag_recved_animal){location_num++;flag_recved_animal=0;}
					}
					FlyDataSend(0,BACK_Mode);					

					set_line(line, path_index, "RED");
					flag_drawpathline=0;
				}
				HAL_Delay(200);
				//update_location();
    }

}

